/*
 * @Author: “ygnnli” “1585923072@qq.com”
 * @Date: 2024-08-08 17:07:57
 * @LastEditors: “ygnnli” “1585923072@qq.com”
 * @LastEditTime: 2024-08-08 17:12:11
 * @FilePath: /81/testNodnBLock.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include "Comm.hpp"
#include <unistd.h>

int main()
{
    char buffer[1024];
    SetNodeBlock(0); // 将标准输入设置为非阻塞
    while (1)
    {
        ssize_t s = ::read(0, buffer, sizeof(buffer)); // 从标准输入读取数据
        if (s > 0)
        {
            buffer[s] = 0; // 字符串结束符
            std::cout << "Echo#: " << buffer << std::endl;
        }
        else
        {
            // 错误为EAGAIN(再试一次)或EWOULDBLOCK(操作将阻塞)
            if (errno == EWOULDBLOCK || errno == EAGAIN)
            {
                std::cout << "底层没有就绪，下次再尝试吧！，做点其他事情！" << std::endl;
                sleep(1);
                continue;
            }
            else if (errno == EINTR) // 被信号中断
            {
                continue;
            }
            else
            {
                std::cerr << "读取错误。。。。 s : " << s << "errno: " << errno << std::endl;
                sleep(1);
            }
        }
    }

    return 0;
}